
// 创建一个SegmentView实例
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var http = require("helper/http");
var circleUrl = require("url/circle");
var circleHelper = require("helper/circle");

var listdata = mm("do_ListData");
var communityUtil = require("helper/community");
var listview = ui("listView");
listview.bindItems(listdata);

var noData = [{"template":1,"labelContent":"没有更多圈子!"}];


//本页面-ajax业务请求url
var httpUrl = circleUrl.url.base.list;//社区更多推荐圈子
var pageCount=1;

//UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
var listView = ui("listView");
//定义变量
var page = 1;
var type;

//andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
//左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
//右上角-
var headerRight = ui(addheader + ".headerRight");
headerRight.visible = false;
headerLeft.on("touch", function() {
	do_App.closePage()
});
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "圈子",
})
header.refreshData();
//页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	binddata();
});
//全部刷新数据
function binddata() {
	pageCount=1;
	var params = {page:pageCount,communityId : communityUtil.communityVisitId()}
	http.post(httpUrl, params, function(data) {
		if (data.circlePage.content) {
			var circleList = data.circlePage.content;
			listview.rebound();
			listdata.removeAll();// 清除数据
			if(circleList.length > 0){
				listdata.addData(circleList);
			}else{
				listdata.addData(noData);
			}
			listview.refreshItems();// 刷新item数据
		}
	});
}
//刷新数据
function getNextPageData() {
	pageCount++;
	var params = {page:pageCount,communityId : communityUtil.communityVisitId()}
	http.post(httpUrl, params, function(data) {
		if (data.circlePage.content) {
			var circleList = data.circlePage.content;
			listview.rebound();
			if(circleList.length > 0){
				listdata.addData(circleList);
			}
			listview.refreshItems();// 刷新item数据
		}
	});
}

//下拉列表，刷新数据
listview.on("pull", function(data) {
	if (data.state == 2) {
		binddata();
		listview.rebound();//复位【一定要做】
	}
});

// 上拉列表，翻页数据
listview.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
		listview.rebound();//复位【一定要做】
	}
});
//点击一个圈子
listview.on("touch", function(data){
	var circelVo = listdata.getOne(data);
	circleHelper.toCircleDetail(circelVo.id,circelVo.name);
});
